package com.pti.usermanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pti.usermanager.dao.api.DeptDao;
import com.pti.usermanager.dao.po.DeptPo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.DeptService;
import com.pti.usermanager.web.dto.request.SaveDeptRequest;
import com.pti.usermanager.web.dto.request.SavePostByDidAndPid;
import com.pti.usermanager.web.dto.request.SaveUserByDidAndUid;
import com.pti.usermanager.web.dto.request.UpdateDeptRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: DeptServiceImpl
 * @Author: liumengyao
 * @Description: 部门业务实现层
 * @Date: 2019/1/17
 */
@Service
public class DeptServiceImpl implements DeptService {

    private static final Logger logger = LogManager.getLogger(DeptServiceImpl.class);

    @Autowired
    private DeptDao deptDao;



    /**
     * @Author liumengyao
     * @Description 分页模糊查部门列表
     * @Date 2019/1/17
     */
    @Override
    public PageInfo<DeptPo> findAll(String deptNm, Integer page, Integer pageSize) {

        logger.debug("deptNm:{} page:{} pageSize:{}", deptNm, page, pageSize);
        //pageHelper工具分页
        PageHelper.startPage(page, pageSize);
        List<DeptPo> deptPoList = deptDao.findAll(deptNm);
        PageInfo<DeptPo> pageInfo = new PageInfo<>(deptPoList);

        return pageInfo;
    }


    /**
     * @Author liumengyao
     * @Description 查看所有数据
     * @Date 2019/1/26
     */
    @Override
    public ResultResponse findAllCanCheck(String deptNm) {

        logger.debug("deptNm:{}" ,deptNm);
        ResultResponse resultResponse = new ResultResponse();

        resultResponse.setData(deptDao.findAllCanCheck(deptNm));
        return resultResponse;
    }


    /**
     * @Author liumengyao
     * @Description 根据部门id查看部门信息
     * @Date 2019/1/26
     */
    @Override
    public ResultResponse findDeptByDeptId(Long deptId) {
        logger.debug("deptId:{}",deptId);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(deptDao.findDeptByDeptId(deptId));
        return resultResponse;
    }


    /**
     * @Author liumengyao
     * @Description 保存部门
     * @Date 2019/1/17
     */
    @Override
    public ResultResponse saveDept(SaveDeptRequest saveDeptRequest) {

        logger.debug("saveDeptRequest:{}", saveDeptRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try {
            deptDao.saveDept(saveDeptRequest);
            return resultResponse;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }


    /**
     * @Author liumengyao
     * @Description 更新部门
     * @Date 2019/1/18
     */
    @Override
    public ResultResponse updateDept(UpdateDeptRequest updateDeptRequest) {

        logger.debug("updateDeptRequest:{}", updateDeptRequest.toString());

        ResultResponse resultResponse = new ResultResponse();

        try {
            deptDao.updateDept(updateDeptRequest);
            return resultResponse;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }


    /**
     * @Author liumengyao
     * @Description 删除部门
     * @Date 2019/1/18
     */
    @Override
    public ResultResponse delDept(Long deptId) {

        logger.debug("deptId:{}", deptId);

        ResultResponse resultResponse = new ResultResponse();

        try {
            deptDao.delDept(deptId);
            return resultResponse;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }


    /**
     * @Author liumengyao
     * @Description 部门分配岗位
     * @Date 2019/1/18
     */
    @Override
    public ResultResponse savePostByDeptIdAndPostId(SavePostByDidAndPid savePostByDidAndPid) {

        logger.debug("savePostByDidAndPid:{}", savePostByDidAndPid.toString());

        ResultResponse resultResponse = new ResultResponse();

        try{
            //删除部门分配岗位关联数据 新增选中的
            deptDao.deletePostByDid(savePostByDidAndPid.getDeptId());
            for(Long postId : savePostByDidAndPid.getPostId()){
                deptDao.savePostByDeptIdAndPostId(savePostByDidAndPid.getDeptId(),postId);
            }
            return resultResponse;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }


    /**
     * @Author liumengyao
     * @Description 部门分配用户
     * @Date 2019/1/18
     */
    @Override
    public ResultResponse saveUserByDeptIdAndUsrId(SaveUserByDidAndUid saveUserByDidAndUid) {
        logger.debug("saveUserByDidAndUid:{}", saveUserByDidAndUid.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            //删除部门分配用户关联数据 新增选中的
            deptDao.deleteUserByDid(saveUserByDidAndUid.getDeptId());
            for(Long usrId : saveUserByDidAndUid.getUsrId()){
                deptDao.saveUserByDeptIdAndUsrId(saveUserByDidAndUid.getDeptId(),usrId);
            }
            return resultResponse;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
            return resultResponse;
        }
    }

    /**
     * @Author mahuilin
     * @Description 部门树查询
     * @Date 2019/1/21
     */
    @Override
    public List<DeptPo> findTree(Long compId) {
        logger.debug("compId:{}", compId);
        List<DeptPo> treeList = deptDao.findTree(compId);
        return treeList;
    }
}
